﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace PIMS_Tools
{
    public class AESEncrypt
    {
        static byte[] Key = new byte[] { 
           0xcb, 0x40, 0xae, 0xdb, 0x6f, 0xe8, 0xb0, 0xbf, 70, 250, 0xde, 0x7f, 0x10, 0x25, 0xf5, 0xcd, 
           0x51, 0xbb, 0xb9, 0xe5, 0x55, 0x20, 0x6d, 0x2d, 0xba, 0x24, 0x19, 0x89, 0x9d, 0xf8, 0xf4, 11
          };
        static byte[] IV = new byte[] { 0xb2, 0x33, 0x19, 0x68, 7, 0x93, 0xc6, 0x45, 0x34, 0xbf, 0x19, 0xf3, 0xd3, 70, 0x83, 0xf2 };

        public static string Decryptor(string str) 
        {
            byte[] buffer;
            using (RijndaelManaged managed = new RijndaelManaged())
            {
                managed.Key = Key;
                managed.IV = IV;
                using (MemoryStream stream3 = new MemoryStream())
                {
                    using (CryptoStream stream4 = new CryptoStream(stream3, managed.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        byte[] a = Convert.FromBase64String(str);
                        stream4.Write(a, 0, a.Length);
                        stream4.Flush();
                        stream4.Close();
                        buffer = stream3.ToArray();
                    }
                }
            }
            return Encoding.UTF8.GetString(buffer);
        }

        public static string Encryptor(string str) 
        {
            byte[] buffer;
            byte[] a = Encoding.UTF8.GetBytes(str);
            using (RijndaelManaged managed = new RijndaelManaged())
            {
                managed.Key = Key;
                managed.IV = IV;
                using (MemoryStream stream3 = new MemoryStream())
                {
                    using (CryptoStream stream4 = new CryptoStream(stream3, managed.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        stream4.Write(a, 0, a.Length);
                        stream4.Flush();
                        stream4.Close();
                        buffer = stream3.ToArray();
                    }
                }
            }
            return Convert.ToBase64String(buffer);
        }
    }
}
